﻿#include "assembleflow.h"
#include "ui_assembleflow.h"

AssembleFlow::AssembleFlow(QWidget *parent) :
    BaseEditor(parent),
    ui(new Ui::AssembleFlow)
{
    ui->setupUi(this);

    table=TD_ASSEMBLE_FLOW;
    selectId=0;

    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);
    ui->listView->setEditTriggers(QListView::NoEditTriggers);

    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);
    ui->tableView->setEditTriggers(QTableView::DoubleClicked);

    updateMonth();
    updateDate();
    updatePartners();
    createMenu();
    updateView();   //手动更新
}

AssembleFlow::~AssembleFlow()
{
    delete ui;
}

void AssembleFlow::generateMenu()
{
    menu->addAction(QStringLiteral("空白新增... (F1)"),this,&AssembleFlow::newItem);
    menu->addAction(QStringLiteral("复制新增... (F2)"),this,&AssembleFlow::copyNewItem);
    menu->addSeparator();

    QMenu *m_update=menu->addMenu(QStringLiteral("更新"));
    m_update->addAction(QStringLiteral("更新日期 (F4)"),this,&AssembleFlow::updateMonth);
    m_update->addAction(QStringLiteral("更新视图 (F5)"),this,&AssembleFlow::updateView);

    QMenu *m_option=menu->addMenu(QStringLiteral("选项"));
    m_option->addAction(QStringLiteral("生成记录"),this,&AssembleFlow::generateSummary);
    m_option->addSeparator();
    m_option->addAction(QStringLiteral("删除 (Delete)"),this,&AssembleFlow::removeItem);

    menu->addSeparator();
    menu->addAction(QStringLiteral("撤销 (Ctrl+Z)"),this,&AssembleFlow::revert);
    menu->addAction(QStringLiteral("保存 (Ctrl+S)"),this,&AssembleFlow::save);
}

void AssembleFlow::keyPressEvent(QKeyEvent *event)
{
    if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_S)
        save();
    else
    {
        switch (event->key()) {
        case Qt::Key_F1:
            newItem();
            break;
        case Qt::Key_F2:
            copyNewItem();
            break;
        case Qt::Key_F4:
            updateDate();
            break;
        case Qt::Key_F5:
            updateView();
            break;
        case Qt::Key_Delete:
            removeItem();
            break;
        }
    }
}

void AssembleFlow::newItem()
{
    AF_AddItem itemAdd(this);
    connect(&itemAdd,&AF_AddItem::accepted,this,&AssembleFlow::updateView);
    if(itemAdd.exec() == QDialog::Accepted)
        updateDate();
}

void AssembleFlow::removeItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(index.isValid())
        model->removeRow(index.row());
}

void AssembleFlow::copyNewItem()
{
    QModelIndex index=ui->tableView->currentIndex();
    if(!index.isValid())
        return;

    // 复制的时候日期也一样
    QString date=sp_fetchString(index,DATE_COL);

    // 这里不需要再判断了
    QString name=sp_fetchString(index,NAME_COL);
    QString number=sp_fetchString(index,NUM_COL);
    QString remark=sp_fetchString(index,PAR_COL);   //备注是用的合作方

    AF_AddItem item(this);
    item.setDate(QDate::fromString(date,DATE_FORMAT));
    item.setWindowTitle(QStringLiteral("加工-新条目"));
    item.setItem(name,number,remark);

    if(item.exec() == QDialog::Accepted)
        updateView();
}

void AssembleFlow::updateMonth()
{
    if(ui->month->count() != 0)
        ui->month->clear();

    QStringList items=sp_getCategory("month_",table);
    if(!items.isEmpty())
    {
        ui->month->addItems(items);
        ui->month->setCurrentIndex(items.size() - 1);
    }
}

void AssembleFlow::updateDate()
{
    if(selectMonth.isEmpty())
        selectMonth=CurrentMonth;

    if(listModel->rowCount() != 0)
        listModel->setStringList(QStringList());

    QStringList items=sp_getCategory("date_",table,"month_",selectMonth);
    if(!items.isEmpty())
        listModel->setStringList(sp_reverseList(items));
}

void AssembleFlow::updatePartners()
{
    QStringList items=sp_getCategory("partner",table);
    if(!items.isEmpty())
    {
        ui->partner->addItems(items);
        ui->partner->setCurrentIndex(-1);
    }
}

void AssembleFlow::on_month_currentTextChanged(const QString &text)
{
    selectMonth=text;
    updateDate();
}

void AssembleFlow::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectDate=index.data().toString();
        updateView();
    }
}

void AssembleFlow::updateView()
{
    if(selectDate.isEmpty())
        selectDate=Today;

    model->setTable(table);
    model->setFilter(QString("date_='%1'").arg(selectDate));
    model->select();

    ui->tableView->hideColumn(ID_COL);   //id
    ui->tableView->hideColumn(MONTH_COL);   //month_
    model->setHeaderData(DATE_COL,Qt::Horizontal,QStringLiteral("日期"));   //date_
    model->setHeaderData(TYP_COL,Qt::Horizontal,QStringLiteral("类型")); //type_
    model->setHeaderData(PAR_COL,Qt::Horizontal,QStringLiteral("合作方")); //partner
    model->setHeaderData(NAME_COL,Qt::Horizontal,QStringLiteral("产品名称")); //name
    ui->tableView->setColumnWidth(NAME_COL,NAME_WIDTH);
    model->setHeaderData(NUM_COL,Qt::Horizontal,QStringLiteral("数量")); //number
    model->setHeaderData(REM_COL,Qt::Horizontal,QStringLiteral("备注")); //remark
}

void AssembleFlow::on_kw_returnPressed()
{
    QString kw=ui->kw->text().trimmed();
    if(kw.isEmpty())
        return;

    model->setTable(table);
    model->setFilter(QString("name like '%%1%'").arg(kw));
    model->select();

    ui->tableView->hideColumn(ID_COL);   //id
    ui->tableView->hideColumn(MONTH_COL);   //month_
    model->setHeaderData(DATE_COL,Qt::Horizontal,QStringLiteral("日期")); //date_
    model->setHeaderData(TYP_COL,Qt::Horizontal,QStringLiteral("类型")); //type_
    model->setHeaderData(PAR_COL,Qt::Horizontal,QStringLiteral("合作方")); //partner
    model->setHeaderData(NAME_COL,Qt::Horizontal,QStringLiteral("产品名称")); //name
    ui->tableView->setColumnWidth(NAME_COL,NAME_WIDTH);
    model->setHeaderData(NUM_COL,Qt::Horizontal,QStringLiteral("数量")); //number
    model->setHeaderData(REM_COL,Qt::Horizontal,QStringLiteral("备注")); //remark
}

void AssembleFlow::on_partner_currentTextChanged(const QString &text)
{
    QString partner=text;

    model->setTable(table);
    model->setFilter(QString("partner like '%%1%'").arg(partner));
    model->select();

    ui->tableView->hideColumn(ID_COL);   //id
    ui->tableView->hideColumn(MONTH_COL);   //month_
    model->setHeaderData(DATE_COL,Qt::Horizontal,QStringLiteral("日期")); //date_
    model->setHeaderData(TYP_COL,Qt::Horizontal,QStringLiteral("类型")); //type_
    ui->tableView->hideColumn(PAR_COL);   //partner
    model->setHeaderData(NAME_COL,Qt::Horizontal,QStringLiteral("产品名称")); //name
    ui->tableView->setColumnWidth(NAME_COL,NAME_WIDTH);
    model->setHeaderData(NUM_COL,Qt::Horizontal,QStringLiteral("数量")); //number
    model->setHeaderData(REM_COL,Qt::Horizontal,QStringLiteral("备注")); //remark
}

void AssembleFlow::generateSummary()
{
    if(selectDate.isEmpty())
        selectDate=Today;

    if(!listModel->stringList().contains(selectDate))
    {
        qApp->clipboard()->setText(QStringLiteral("今日无记录！"));
        return;
    }

    QSqlQuery query;
    query.exec(QString("SELECT type_,name,number FROM %1 WHERE date_='%2' ORDER BY type_")
               .arg(table)
               .arg(selectDate));
    QStringList items;
    items.append(selectDate + QStringLiteral(" 加工记录："));
    while(query.next())
    {
        QString type=query.value(0).toString();
        QString name=query.value(1).toString();
        if(name.isEmpty())
            continue;

        int i_number=query.value(2).toInt();
        if(i_number == 0)
            continue;

        QString number=QString::number(i_number);
        QString item=name + QString(" ") + type + QString(" ") + number;
        items.append(item);
    }

    if(!items.isEmpty())
    {
        QString summary=items.join("\n");
        qApp->clipboard()->setText(summary);
    }
    else
        qApp->clipboard()->setText(QStringLiteral("今日无记录！"));
}
